// miniprogram/pages/twoPage/twoPage.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: {
      slider: "",
      switch: "",
      input: "",
      radio: "",
      checkbox: ""
    },
    array: ['美国', '中国', '巴西', '日本'],
    objectArray: [{
        id: 0,
        name: '美国'
      },
      {
        id: 1,
        name: '中国'
      },
      {
        id: 2,
        name: '巴西'
      },
      {
        id: 3,
        name: '日本'
      }
    ],
    index: 0,
    multiArray: [
      ['无脊柱动物', '脊柱动物'],
      ['扁性动物', '线形动物', '环节动物', '软体动物', '节肢动物'],
      ['猪肉绦虫', '吸血虫']
    ],
    objectMultiArray: [
      [{
          id: 0,
          name: '无脊柱动物'
        },
        {
          id: 1,
          name: '脊柱动物'
        }
      ],
      [{
          id: 0,
          name: '扁性动物'
        },
        {
          id: 1,
          name: '线形动物'
        },
        {
          id: 2,
          name: '环节动物'
        },
        {
          id: 3,
          name: '软体动物'
        },
        {
          id: 3,
          name: '节肢动物'
        }
      ]
    ],
    multiIndex: [0, 0, 0],
    date: '2016-09-01',
    time: '12:01',
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部',
    images: [],
    waterImage: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var value = JSON.parse(options.value);
    this.setData({
      slider: value.slider,
      switch: value.switch,
      input: value.input,
      radio: value.radioGroup,
      checkbox: value.checkbox
    })
    console.log(JSON.parse(options.value))
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindMultiPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  bindMultiPickerColumnChange(e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value)
    const data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    }
    data.multiIndex[e.detail.column] = e.detail.value
    switch (e.detail.column) {
      case 0:
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['扁性动物', '线形动物', '环节动物', '软体动物', '节肢动物']
            data.multiArray[2] = ['猪肉绦虫', '吸血虫']
            break
          case 1:
            data.multiArray[1] = ['鱼', '两栖动物', '爬行动物']
            data.multiArray[2] = ['鲫鱼', '带鱼']
            break
        }
        data.multiIndex[1] = 0
        data.multiIndex[2] = 0
        break
      case 1:
        switch (data.multiIndex[0]) {
          case 0:
            switch (data.multiIndex[1]) {
              case 0:
                data.multiArray[2] = ['猪肉绦虫', '吸血虫']
                break
              case 1:
                data.multiArray[2] = ['蛔虫']
                break
              case 2:
                data.multiArray[2] = ['蚂蚁', '蚂蟥']
                break
              case 3:
                data.multiArray[2] = ['河蚌', '蜗牛', '蛞蝓']
                break
              case 4:
                data.multiArray[2] = ['昆虫', '甲壳动物', '蛛形动物', '多足动物']
                break
            }
            break
          case 1:
            switch (data.multiIndex[1]) {
              case 0:
                data.multiArray[2] = ['鲫鱼', '带鱼']
                break
              case 1:
                data.multiArray[2] = ['青蛙', '娃娃鱼']
                break
              case 2:
                data.multiArray[2] = ['蜥蜴', '龟', '壁虎']
                break
            }
            break
        }
        data.multiIndex[2] = 0
        break
    }
    console.log(data.multiIndex)
    this.setData(data)
  },
  bindDateChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  canvasIdErrorCallback(e) {
    console.error(e.detail.errMsg)
  },
  formSubmit(e) {
    console.log("选择器提交的数据为：", JSON.stringify(e.detail.value))
    wx.showToast({
      title: JSON.stringify(e.detail.value),
    })
  },
  callPhone(e) {
    console.log("输入的电话号码为:::", e.detail.value.phone)
    wx.makePhoneCall({
      phoneNumber: e.detail.value.phone,
    })
    wx.vibrateLong({

    })
  },
  // 打开图库
  openpic: function() {
    var that = this;
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        wx.showToast({
          title: "图片张数:" + tempFilePaths.length,
          icon: 'success',
          duration: 2000
        })
        console.log("获取到的图片路径为", tempFilePaths)
        that.setData({
          images: that.data.images.concat(tempFilePaths)
        })
      }
    })
  },
  lookBigImage: function(event) {
    console.log("查看大图")
    var that = this
    var index = event.currentTarget.dataset.index;
    wx.previewImage({
      current: that.data.images[index], // 当前显示图片的http链接
      urls: that.data.images // 需要预览的图片http链接列表
    })
  },
  openThree: function() {
    wx.navigateTo({
      url: '/mulu1/three/threePage',
    })
  },
  //添加水印
  addWaterToImage: function() {
    var height = 260;
    var weight = 60;
    const context = wx.createCanvasContext('firstCanvas')
    context.drawImage("../../images/pic.png", 50, 50, 300, 300)
    context.drawImage("../../images/picc.png", weight, height);
    context.setFontSize(12)
    context.fillText('时间:2018-12-15', weight, height + 45)
    context.fillText('地址:北京市海淀区靠近联想大厦', weight, height + 60)
    context.fillText('操作人:陈业熊', weight, height + 75)
    context.draw()
  },
  //保存水印图片
  saveWaterImage: function() {
    var that = this
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 400,
      height: 400,
      destWidth: 400,
      destHeight: 400,
      canvasId: 'firstCanvas',
      success(res) {
        that.setData({
          waterImage: res.tempFilePath
        })
      }
    })
  }
})